<?php
class admin_approve extends Admin_Controller
{
	public $menu_id = 17;
	public function __construct()
	{
		parent::__construct();
		$this->load->model('users/users_model','user');
		$this->load->model('approve/approve_model','approve');
		$this->load->model('member/request_model','request');
		$this->load->model('member/request_con_model','request_con');
	}
	
	public function index()
	{
		$menu_id = $this->menu_id;
		$data['menu_id'] = $menu_id;		
		$menu_name = GetMenuProperty($menu_id,'title');
		$data['urlpage']="admin_approve";
		if(is_login()){
			if(permission($menu_id, 'canview')=='')redirect('admin');
			$condition = "1=1";
			//$condition .= @$_GET['type']!='' ? " AND page='".$_GET['type']."'" : "";
			$data['requests'] = $this->request->get();
			$data['pagination'] = $this->request->pagination();
			$this->template->build('index',$data);
		}
		else{
			
			redirect('admin');	
		}
	}
	public function form($id=FALSE){
		$menu_id=$this->menu_id;
		$curr_menu = $this->db->getone("SELECT title FROM admin_menu where id=".$menu_id);
		$this->template->append_metadata(js_datepicker());
		$data['detail'] = @$this->request->get_row($id);
		$data['request_cons'] = $this->request_con->where('request_id = '.$id)->order_by('id','asc')->limit(9999)->get();
		if($id>0){
		//$action='View';
		//$description = $action.' '.$curr_menu.' : '.$data['rs']['Title'];		
		//save_log($menu_id,$action,$description);			
		}
		//$data['detail'] = @$this->db->getrow("SELECT * FROM tbknowledge WHERE id=".$data['rs']['pid']);
		$this->template->build('form',$data);
	}

	function request_con_save(){
		if($_POST){
			
			// บันทึกคำขอ
			if($_POST['detail']  != ""){
				$_POST['created'] = date("Y-m-d H:i:s");
				$_POST['sender'] = login_data('id');
				$this->request_con->save($_POST);
				
				$user = $this->user->get_row($_POST['recipients']);
				
				$type = ($_POST['type'] == 'request')?"approve_detail":"request_detail";
				$link = $_POST['mail_url'].'/phklb/member/'.$type.'/'.$_POST['request_id'];
				
				$this->send_mail_phpmailer($user['email'],$_POST['mail_kntitle'],$_POST['mail_author'],$_POST['detail'],$link);
			}
			
			// ยันทึกสถานะ
			$_POST['id'] = $_POST['request_id'];
			$this->request->save($_POST);
			
			set_notify('success', 'บันทึกข้อมูลเรียบร้อยแล้ว');
		}
		redirect($_POST['referer']);
	}
	
	function send_mail_phpmailer($email,$kn_title,$author,$detail,$link){
		require("include/PHPMailer/class.phpmailer.php");
		$mail = new PHPMailer();
		
		$body = 'ขออนุญาตใช้ข้อมูล<br><br>';
		$body .= 'เรื่อง : '.$kn_title.'<br><br>';
		$body .= 'ผู้แต่ง/ผู้จัดทำ : '.$author.'<br><br>';
		$body .= 'ข้อความ : '.$detail.'<br><br>';
		$body .= '<a href="'.$link.'">'.$link.'</a>';
		
		$mail->CharSet = "utf-8";
		$mail->IsSMTP();
		$mail->SMTPDebug = 0;
		$mail->SMTPAuth = true;
		$mail->Host       = "smtp.mail.yahoo.com";
		$mail->Port       = 25;
		$mail->Username   = "favouritedesign18@yahoo.com";
		$mail->Password   = "55555555";
		
		$mail->SetFrom('favouritedesign18@yahoo.com', 'webmaster@ph.mahidol.ac.th');
		$mail->AddReplyTo("favouritedesign18@yahoo.com", "favouritedesign18@yahoo.com");
		$mail->Subject = "ขออนุญาตใช้งานข้อมูล - ระบบคลังความรู้เพื่อสนับสนุนงานสาธารณสุข คณะสาธารณสุขศาสตร์ มหาวิทยาลัยมหิดล";
		
		$mail->MsgHTML($body);
		
		$mail->AddAddress($email, $email); // ผู้รับคนที่หนึ่ง
		
		if(!$mail->Send()) {
		    echo "Mailer Error: " . $mail->ErrorInfo;
		} else {
		    echo "Message sent!";
		}
	}


	public function save(){
		//$this->db->debug = true;
		$menu_id=17;	
		$menu_name = GetMenuProperty($menu_id,'title');
		if($_POST['id']!='')
		{
			if(permission($menu_id, 'canedit')=='')redirect('admin_approve');
		//	$action='Update';
		//	$description = $action.' '.$menu_name.' : '.$_POST['Title'];		
		//	save_log($menu_id,$action,$description);
		}else{
			if(permission($menu_id, 'canadd')=='')redirect('admin_approve');	
			//$action='Add';
			//$description = $action.' '.$menu_name.' : '.$_POST['Title'];		
			//save_log($menu_id,$action,$description);
		}	
		if(isset($_POST['request_date']))$_POST['request_date'] = Date2DB($_POST['request_date']);	
		if(isset($_POST['approve_start_date']))$_POST['approve_start_date'] = Date2DB($_POST['approve_start_date']);	
		if(isset($_POST['approve_end_date']))$_POST['approve_end_date'] = Date2DB($_POST['approve_end_date']);	
		$id = $this->approve->save($_POST);		
		set_notify('success', lang('save_data_complete'));
		redirect('admin_approve');
	} 
	function delete($id=FALSE){
		$menu_id=17;
		$menu_name = GetMenuProperty($menu_id,'title');
		if(permission($menu_id, 'candelete')=='')redirect('admin_approve');
		if($id){
			//$data = $this->approve->get_row($id);
			//$action='Delete';
			//$description = $action.' '.$menu_name.' : '.$data['Title'];		
			//save_log($menu_id,$action,$description);
			$this->approve->delete($id);
			set_notify('success', lang('delete_data_complete'));
		}
		redirect('admin_approve');
	}
}
?>